<?php

namespace App\Exceptions;

use Illuminate\Support\Facades\Validator;

/**
 * 异常信息
 * Class ExceptionSms
 * @package App\Exceptions
 * @author: lisa
 * @time: 2020/12/15 16:41
 */
class ExceptionSms
{
    const PWD_ERROR = '密码输入错误';
    const LOGIN_EXCEPTION = '登录异常';
    const LOGIN_FORBIDDEN = '账户被禁用';
    const LOGIN_EXPIRED   = '登录过期';
    const SYSTEM_ERROR    = '系统繁忙，请稍后重试';
    const FILE_UP_ERROR   = '文件上传失败';

    /**
     * 返回成功信息
     * @name:returnSuccess
     * @param array $data 数据
     * @return false|string
     * @author: lisa
     * @datetime: 2020/12/15 16:41
     */
    public static function returnSuccess($data = [], $count = 0, $msg = '提交成功')
    {
        return (json_encode([
            'code'    => 200,
            'data'    => $data,
            'message' => $msg,
            'count'   => $count
        ]));
    }

    /**
     * 返回错误异常
     * @name:returnError
     * @param string $msg
     * @author: lisa
     * @datetime: 2020/12/15 16:41
     */
    public static function returnError($msg = '操作失败',$code = null)
    {
        if ($code) {
            if (in_array($code, array_keys(ExceptionHelper::$msg))) {
                throw ExceptionHelper::getInstance($code, $msg);
            } else {
                throw ExceptionHelper::getInstance(ExceptionHelper::DEFAULT_ERROR, '操作失败');
            }
        } else {
            throw ExceptionHelper::getInstance(ExceptionHelper::DEFAULT_ERROR, $msg);
        }
    }

    /**
     * 检测请求参数
     * @name:checkParams
     * @param array $input 请求参数
     * @param array $rules 验证规则
     * @param array $attrMsg 参数属性名称
     * @author: lisa
     * @datetime: 2020/12/15 16:40
     */
    public static function checkParams($input = [], $rules = [], $attrMsg = [])
    {
        $attrMsg['page']  = '页数';
        $attrMsg['limit'] = '条数';
        $attrMsg['pay_type'] = '支付方式';

        $validate = Validator::make($input, $rules, [], $attrMsg);

        $errors = $validate->errors()->all();

        if (!empty($errors)) self::returnError($errors[0]);
    }

    /**
     * 验证返回信息
     * @name:validateMsg
     * @return array
     * @author: lisa
     * @datetime: 2020/12/15 16:42
     */
    public static function validateMsg()
    {
        $msg = [
            'required'           => ':attribute不能为空',
            'url'                => ':attribute请填写以http://或https://开头的链接',
            'in'                 => ':attribute无效',
            'integer'            => ':attribute请填写整数',
            'numeric'            => ':attribute请填写数字',
            'date_format'        => ':attribute请填写日期格式',
            'email'              => ':attribute请填写正确格式的邮箱，格式如：ldd@qq.com',
            'api_address_exists' => '该收货地址已不存在',
            'unique'             => ':attribute已存在',
            'price'              => ':attribute请保留小数点后两位',
            'car_num'            => '车牌号填写格式错误，格式如：川A123AB',
            'real_name'          => '姓名填写格式错误，请输入2~6位中文',
            'id_card_num'        => '身份证号填写格式错误，格式如：51303019960528XXXX',
            'captcha'            => ':attribute错误',
            'exists'             => ':attribute不存在',
            'present'            => '缺少:attribute参数',
            'verify_code'        => ':attribute必须为包含数字的字符串',
            'pass_word'          => ':attribute为包含数字、字母或一些特殊字符的6~20位字符',
            'alpha_num'          => ':attribute必须为包含数字、字母的字符串',
            'phone'              => ':attribute必须为由[11|12|13|14|15|16|17|18|19]开头的11位数字字符',
            'contract_tel'       => ':attribute必须有效手机号或座机号（区号和号码以“-”间隔）',

            'size' => [
                'string' => ':attribute必须为:size位字符'
            ],
            'between' => [
                'string' => ':attribute必须是:min~:max位字符'
            ],

            'min' => [
                'numeric' => ':attribute至少为:min',
                'integer' => ':attribute至少为:min',
                'string'  => ':attribute至少:min个字',
            ],
            'max' => [
                'numeric' => ':attribute最大为:max',
                'integer' => ':attribute最大为:max',
                'string'  => ':attribute最多:max个字',
            ],
        ];
        return $msg;
    }

    /**
     * 后台返回输出
     * @name:homeList
     * @param array $data 数据
     * @param int $count 总数
     * @param string $msg 信息
     * @param int $code 状态码
     * @return array
     * @author: lisa
     * @datetime: 2020/12/15 16:42
     */
    public static function homeList($data, $count = 10, $msg = '', $code = 0)
    {
        $return = [
            'code'  => $code,
            'msg'   => $msg,
            'count' => $count,
            'data'  => $data
        ];
        return $return;
    }
}
